package com.yupi.springbootinit.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.mapper.PostMapper;
import com.yupi.springbootinit.mapper.UserMapper;
import com.yupi.springbootinit.model.dto.picture.PictureRequest;
import com.yupi.springbootinit.model.dto.post.PostQueryRequest;
import com.yupi.springbootinit.model.dto.search.SearchAllRequest;
import com.yupi.springbootinit.model.entity.Picture;
import com.yupi.springbootinit.model.entity.Post;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.SearchAllVO;
import com.yupi.springbootinit.service.PictureService;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/10/8 23:52
 **/
@RestController
@RequestMapping("/search")
public class SearchController {

    @Resource
    private PostService postService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private PictureService pictureService;

    @PostMapping("/all")
    public BaseResponse searchAll(@RequestBody SearchAllRequest searchAllRequest) {
        String searchText = searchAllRequest.getText();

        CompletableFuture<List<Post>> postFuture = CompletableFuture.supplyAsync(() -> {
            PostQueryRequest postQueryRequest = new PostQueryRequest();
            postQueryRequest.setSearchText(searchText);
            postQueryRequest.setTitle(searchText);
            postQueryRequest.setContent(searchText);

            List<Post> postList = postService.searchFromEs(postQueryRequest);
            return postList;
        });


        CompletableFuture<List<Picture>> pictureFuture = CompletableFuture.supplyAsync(() -> {
            PictureRequest pictureRequest = new PictureRequest();
            pictureRequest.setSearch(searchText);
            pictureRequest.setPageNum(1);
            List<Picture> pictureList = pictureService.getPicture(pictureRequest);
            return pictureList;
        });
        CompletableFuture<List<User>> userFuture = CompletableFuture.supplyAsync(() -> {
            LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
            userQueryWrapper.like(User::getUserName, searchText)
                    .or()
                    .like(User::getUserAccount, searchText)
                    .or()
                    .like(User::getUserProfile, searchText);
            List<User> userList = userMapper.selectList(userQueryWrapper);
            return userList;
        });

        SearchAllVO searchAllVO = new SearchAllVO();
        try {
            searchAllVO.setPostList(postFuture.get());
            searchAllVO.setPictureList(pictureFuture.get());
            searchAllVO.setUserList(userFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        return ResultUtils.success(searchAllVO);
    }

}
